<template>
  <div class="profile-page">
    <!-- 用户信息卡片 -->
    <div class="user-card">
      <div class="user-info">
        <img :src="userInfo.avatar || defaultAvatar" class="user-avatar" />
        <div class="user-details">
          <div class="user-name">{{ userInfo.nickname || '未设置昵称' }}</div>
          <div class="user-phone">{{ formatPhone(userInfo.phone) }}</div>
          <div class="user-location">{{ userInfo.location || '未设置地区' }}</div>
        </div>
        <van-icon name="arrow" class="arrow-icon" @click="goToProfile" />
      </div>
      
      <!-- 用户统计 -->
      <div class="user-stats">
        <div class="stat-item" @click="goToMyPosts">
          <div class="stat-number">{{ userStats.postCount }}</div>
          <div class="stat-label">我的发布</div>
        </div>
        <div class="stat-item" @click="goToMyCollects">
          <div class="stat-number">{{ userStats.collectCount }}</div>
          <div class="stat-label">我的收藏</div>
        </div>
        <div class="stat-item" @click="goToMyProgress">
          <div class="stat-number">{{ userStats.progressCount }}</div>
          <div class="stat-label">维权进度</div>
        </div>
      </div>
    </div>

    <!-- 快捷功能 -->
    <div class="quick-actions">
      <div class="section-title">快捷功能</div>
      <div class="action-grid">
        <div class="action-item" @click="goToAIDiagnosis">
          <div class="action-icon">🤖</div>
          <div class="action-name">AI诊断</div>
        </div>
        <div class="action-item" @click="goToTemplates">
          <div class="action-icon">📄</div>
          <div class="action-name">模板库</div>
        </div>
        <div class="action-item" @click="goToAssistance">
          <div class="action-icon">🆘</div>
          <div class="action-name">找援助</div>
        </div>
        <div class="action-item" @click="goToCommunity">
          <div class="action-icon">👥</div>
          <div class="action-name">社区</div>
        </div>
      </div>
    </div>

    <!-- 我的维权 -->
    <div class="my-rights">
      <div class="section-header">
        <div class="section-title">我的维权</div>
        <div class="section-more" @click="goToMyProgress">查看全部</div>
      </div>
      
      <div v-if="recentProgress.length === 0" class="empty-state">
        <div class="empty-icon">📋</div>
        <div class="empty-text">暂无维权记录</div>
        <van-button type="primary" size="small" @click="goToAIDiagnosis">
          开始维权诊断
        </van-button>
      </div>
      
      <div v-else class="progress-list">
        <div
          v-for="progress in recentProgress"
          :key="progress.id"
          class="progress-item"
          @click="goToProgressDetail(progress.id)"
        >
          <div class="progress-header">
            <div class="progress-title">{{ progress.title }}</div>
            <div class="progress-status" :class="progress.status">
              {{ getStatusText(progress.status) }}
            </div>
          </div>
          <div class="progress-info">
            <div class="progress-category">{{ progress.category }}</div>
            <div class="progress-time">{{ formatTime(progress.updateTime) }}</div>
          </div>
          <div class="progress-bar">
            <van-progress
              :percentage="(progress.currentStep / progress.totalSteps) * 100"
              color="#1989fa"
              stroke-width="4"
            />
            <div class="progress-text">
              第{{ progress.currentStep }}步 / 共{{ progress.totalSteps }}步
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 功能菜单 -->
    <div class="menu-section">
      <van-cell-group>
        <van-cell
          title="我的收藏"
          icon="star-o"
          is-link
          @click="goToMyCollects"
        />
        <van-cell
          title="我的发布"
          icon="chat-o"
          is-link
          @click="goToMyPosts"
        />
        <van-cell
          title="使用记录"
          icon="records"
          is-link
          @click="goToUsageHistory"
        />
        <van-cell
          title="意见反馈"
          icon="comment-o"
          is-link
          @click="goToFeedback"
        />
      </van-cell-group>
    </div>

    <!-- 设置菜单 -->
    <div class="menu-section">
      <van-cell-group>
        <van-cell
          title="个人资料"
          icon="manager-o"
          is-link
          @click="goToProfile"
        />
        <van-cell
          title="隐私设置"
          icon="shield-o"
          is-link
          @click="goToPrivacy"
        />
        <van-cell
          title="消息通知"
          icon="bell"
          is-link
          @click="goToNotification"
        />
        <van-cell
          title="关于我们"
          icon="info-o"
          is-link
          @click="goToAbout"
        />
      </van-cell-group>
    </div>

    <!-- 退出登录 -->
    <div class="logout-section">
      <van-button
        type="danger"
        size="large"
        block
        @click="showLogoutConfirm = true"
      >
        退出登录
      </van-button>
    </div>

    <!-- 底部导航 -->
    <van-tabbar v-model="activeTab">
      <van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
      <van-tabbar-item icon="chat-o" to="/community">社区</van-tabbar-item>
      <van-tabbar-item icon="orders-o" to="/templates">工具</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/profile">我的</van-tabbar-item>
    </van-tabbar>

    <!-- 退出确认弹窗 -->
    <van-dialog
      v-model:show="showLogoutConfirm"
      title="确认退出"
      message="确定要退出登录吗？"
      show-cancel-button
      @confirm="handleLogout"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { showToast, showConfirmDialog } from 'vant'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const activeTab = ref(3)
const showLogoutConfirm = ref(false)
const defaultAvatar = 'https://img.yzcdn.cn/vant/cat.jpeg'

// 用户信息
const userInfo = computed(() => userStore.userInfo || {})

// 用户统计数据
const userStats = reactive({
  postCount: 5,
  collectCount: 12,
  progressCount: 3
})

// 最近维权进度
const recentProgress = ref([
  {
    id: 1,
    title: '公司拖欠工资维权',
    category: '职场纠纷',
    status: 'ongoing',
    currentStep: 2,
    totalSteps: 4,
    updateTime: '2024-01-15 14:30:00'
  },
  {
    id: 2,
    title: '租房押金退还',
    category: '租房矛盾',
    status: 'completed',
    currentStep: 3,
    totalSteps: 3,
    updateTime: '2024-01-10 09:15:00'
  }
])

// 方法
const formatPhone = (phone) => {
  if (!phone) return '未绑定手机'
  return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

const formatTime = (timeStr) => {
  const time = new Date(timeStr)
  const now = new Date()
  const diff = now - time
  
  if (diff < 86400000) return '今天'
  if (diff < 2592000000) return `${Math.floor(diff / 86400000)}天前`
  
  return timeStr.split(' ')[0]
}

const getStatusText = (status) => {
  const statusMap = {
    ongoing: '进行中',
    completed: '已完成',
    abandoned: '已放弃'
  }
  return statusMap[status] || '未知'
}

// 导航方法
const goToProfile = () => {
  router.push('/profile/edit')
}

const goToMyPosts = () => {
  router.push('/profile/posts')
}

const goToMyCollects = () => {
  router.push('/profile/collects')
}

const goToMyProgress = () => {
  router.push('/profile/progress')
}

const goToProgressDetail = (id) => {
  router.push(`/profile/progress/${id}`)
}

const goToUsageHistory = () => {
  router.push('/profile/history')
}

const goToFeedback = () => {
  router.push('/profile/feedback')
}

const goToPrivacy = () => {
  router.push('/profile/privacy')
}

const goToNotification = () => {
  router.push('/profile/notification')
}

const goToAbout = () => {
  router.push('/profile/about')
}

const goToAIDiagnosis = () => {
  router.push('/ai/diagnosis')
}

const goToTemplates = () => {
  router.push('/templates')
}

const goToAssistance = () => {
  router.push('/assistance')
}

const goToCommunity = () => {
  router.push('/community')
}

const handleLogout = async () => {
  try {
    await userStore.logout()
    showToast('已退出登录')
    router.push('/login')
  } catch (error) {
    showToast('退出失败，请重试')
  }
}

onMounted(() => {
  // 如果未登录，跳转到登录页
  if (!userStore.isLoggedIn) {
    router.push('/login')
    return
  }
  
  // 加载用户数据
  loadUserData()
})

const loadUserData = async () => {
  try {
    // TODO: 加载用户统计数据和维权进度
    console.log('加载用户数据')
  } catch (error) {
    console.error('加载用户数据失败:', error)
  }
}
</script>

<style scoped>
.profile-page {
  min-height: 100vh;
  background: #f7f8fa;
  padding-bottom: 50px;
}

.user-card {
  background: linear-gradient(135deg, #1989fa 0%, #1890ff 100%);
  color: white;
  padding: 24px 16px;
  margin-bottom: 8px;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}

.user-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-right: 16px;
  border: 3px solid rgba(255, 255, 255, 0.3);
}

.user-details {
  flex: 1;
}

.user-name {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 4px;
}

.user-phone {
  font-size: 14px;
  opacity: 0.8;
  margin-bottom: 2px;
}

.user-location {
  font-size: 12px;
  opacity: 0.7;
}

.arrow-icon {
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
}

.user-stats {
  display: flex;
  justify-content: space-around;
}

.stat-item {
  text-align: center;
  cursor: pointer;
}

.stat-number {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  opacity: 0.8;
}

.quick-actions {
  background: white;
  padding: 16px;
  margin-bottom: 8px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #323233;
  margin-bottom: 16px;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.action-item {
  text-align: center;
  cursor: pointer;
}

.action-icon {
  font-size: 32px;
  margin-bottom: 8px;
}

.action-name {
  font-size: 12px;
  color: #646566;
}

.my-rights {
  background: white;
  padding: 16px;
  margin-bottom: 8px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-more {
  font-size: 14px;
  color: #1989fa;
  cursor: pointer;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 14px;
  color: #969799;
  margin-bottom: 20px;
}

.progress-list {
  space-y: 16px;
}

.progress-item {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 16px;
  cursor: pointer;
  margin-bottom: 16px;
  transition: all 0.2s;
}

.progress-item:hover {
  border-color: #1989fa;
  background: #f0f9ff;
}

.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.progress-title {
  font-size: 14px;
  font-weight: 600;
  color: #323233;
}

.progress-status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
}

.progress-status.ongoing {
  background: #fff7e6;
  color: #ff976a;
}

.progress-status.completed {
  background: #f6ffed;
  color: #52c41a;
}

.progress-status.abandoned {
  background: #fff2f0;
  color: #ff4d4f;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
}

.progress-category {
  font-size: 12px;
  color: #1989fa;
  background: #f0f9ff;
  padding: 2px 6px;
  border-radius: 3px;
}

.progress-time {
  font-size: 12px;
  color: #969799;
}

.progress-bar {
  margin-bottom: 8px;
}

.progress-text {
  font-size: 12px;
  color: #646566;
  text-align: right;
  margin-top: 4px;
}

.menu-section {
  margin-bottom: 8px;
}

.logout-section {
  padding: 16px;
  margin-top: 24px;
}
</style>